package com.roadjava.javaweb.student.servlet;

import com.roadjava.javaweb.student.beans.entity.StudentDO;
import com.roadjava.javaweb.student.beans.req.StudentRequest;
import com.roadjava.javaweb.student.beans.res.ResultDTO;
import com.roadjava.javaweb.student.beans.res.TableResult;
import com.roadjava.javaweb.student.beans.vo.StudentVO;
import com.roadjava.javaweb.student.service.StudentService;
import com.roadjava.javaweb.student.service.impl.StudentServiceImpl;
import com.roadjava.javaweb.student.util.ResponseUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "studentServlet",urlPatterns = "/studentServlet")
public class StudentServlet extends HttpServlet {
    private StudentService studentService = new StudentServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String type = request.getParameter("type");
        if ("toStudentManage".equals(type)) {
            // 查询出来学生列表放到request中
            String studentName = request.getParameter("studentName");
            String pageNowStr = request.getParameter("pageNow");//当前第几页
            StudentRequest studentRequest = new StudentRequest();
            int pageNow = 1; // 默认查第一页
            if (pageNowStr != null && !"".equals(pageNowStr.trim())) {
                pageNow = Integer.parseInt(pageNowStr);
            }
            studentRequest.setPageNow(pageNow);
            studentRequest.setStudentName(studentName);
            TableResult<StudentVO> tableResult =studentService.queryStudentByPage(studentRequest);
            tableResult.setPageNow(pageNow);
            tableResult.setStudentName(studentName == null ? "" : studentName);
            // 放到request请求域中，并在studentManage.jsp中使用
            request.setAttribute("tableResult",tableResult);
            request.getRequestDispatcher("/WEB-INF/student/studentManage.jsp").forward(request,response);
        }else if("toAdd".equals(type)) {
            request.getRequestDispatcher("/WEB-INF/student/add.jsp").forward(request,response);
        }else if("add".equals(type)) {
            // 执行学生的添加
            String studentName = request.getParameter("studentName");
            String no = request.getParameter("no");
            String birthDay = request.getParameter("birthDay");
            String maj = request.getParameter("maj");
            StudentDO studentDO = new StudentDO();
            studentDO.setName(studentName);
            studentDO.setNo(no);
            studentDO.setBirthDay(birthDay);
            studentDO.setMaj(maj);
            studentService.addStudent(studentDO);
            response.sendRedirect(request.getContextPath()+"/studentServlet?type=toStudentManage");
        }else if ("toUpdate".equals(type)) {
            String studentId = request.getParameter("id");
            String pageNow = request.getParameter("pageNow");
            StudentVO studentVO = studentService.getStudentById(Long.parseLong(studentId));
            request.setAttribute("studentVO",studentVO);
            request.setAttribute("pageNow",Integer.parseInt(pageNow));
            request.getRequestDispatcher("/WEB-INF/student/update.jsp").forward(request,response);
        }else if ("update".equals(type)) {
            String studentId = request.getParameter("studentId");
            String studentName = request.getParameter("studentName");
            String no = request.getParameter("no");
            String birthDay = request.getParameter("birthDay");
            String maj = request.getParameter("maj");
            String pageNow = request.getParameter("pageNow");
            // 把参数封装为对象
            StudentDO studentDO = new StudentDO();
            studentDO.setId(Long.parseLong(studentId));
            studentDO.setName(studentName);
            studentDO.setNo(no);
            studentDO.setBirthDay(birthDay);
            studentDO.setMaj(maj);
            studentService.updateStudent(studentDO);
            response.sendRedirect(request.getContextPath()+"/studentServlet?type=toStudentManage&pageNow="+pageNow);
        }else if ("delete".equals(type)) {
            String studentId = request.getParameter("id");
            studentService.deleteStudentById(Long.parseLong(studentId));
            response.sendRedirect(request.getContextPath()+"/studentServlet?type=toStudentManage");
        }else if ("loadAllStudents".equals(type)) {
            ResultDTO<List<StudentVO>> resultDTO = studentService.loadAllStudents();
            ResponseUtil.respAppJson(response,resultDTO);
        }
    }
}
